<?php
    class Clip extends CActiveRecord
    {
        /* Lấy video hot */
        public function getVideoHot($currentPage, $numberRecordPerPage)
        {
            $cacheService = new CacheService("Clip","getVideoHot",$currentPage.$numberRecordPerPage);
            $key = $cacheService->createKey();
            $cache = Yii::app()->cache->get($key);
            //$cache = false;
            if ($cache == false) 
            {
                $startRecord = ($currentPage - 1) * $numberRecordPerPage;  
                $sql = "SELECT id, title,picture,introtext,file,file_mp4,alias,picture,hit,download,duration,isHot,create_date FROM c_video WHERE  status=1 AND isHot =1 AND categoryId NOT IN (10,18) ORDER BY create_date DESC  LIMIT " . $startRecord . ", " . $numberRecordPerPage;
                $command = Yii::app()->db_kkt->createCommand($sql);
                $rows = $command->queryAll();
                Yii::app()->cache->set($key, $rows, ConstantsUtil::TIME_CACHE_1800); 
            }else{ 
                $rows=$cache;  
            }
            return $rows;
        }


        public function getTotalVideoHot()
        {
            $sql = "SELECT count(id) FROM c_video WHERE status=1 AND isHot=1 AND categoryId NOT IN (10,18)  ";
            $command = Yii::app()->db_kkt->createCommand($sql);
            $rows = $command->queryRow();        
            return $rows["count(id)"];
        }
        /* Lấy video new */
        public function getVideoNew($currentPage, $numberRecordPerPage)
        {
            $cacheService = new CacheService("Clip","getVideoNew",$currentPage.$numberRecordPerPage);
            $key = $cacheService->createKey();
            $cache = Yii::app()->cache->get($key);
            //$cache == false;
            if ($cache == false) 
            {
                $startRecord = ($currentPage - 1) * $numberRecordPerPage;  
                $sql = "SELECT id, title,picture,introtext,file,file_mp4,alias,picture,hit,download,duration,isHot,create_date FROM c_video WHERE  status=1 AND categoryId NOT IN (10,18) ORDER BY create_date DESC  LIMIT " . $startRecord . ", " . $numberRecordPerPage;
                $command = Yii::app()->db_kkt->createCommand($sql);
                $rows = $command->queryAll();
                Yii::app()->cache->set($key, $rows, ConstantsUtil::TIME_CACHE_1800); 
            }else{ 
                $rows=$cache;  
            }
            return $rows;
        }
        /* Lấy video top */
        public function getVideoTop($currentPage, $numberRecordPerPage)
        {
            $cacheService = new CacheService("Clip","getVideoTop",$currentPage.$numberRecordPerPage);
            $key = $cacheService->createKey();
            $cache = Yii::app()->cache->get($key);
            //$cache = false;
            if ($cache == false) 
            {
                $startRecord = ($currentPage - 1) * $numberRecordPerPage;  
                $sql = "SELECT id, title,picture,introtext,file,file_mp4,alias,picture,hit,download,duration,isHot,create_date FROM c_video WHERE  status=1 AND categoryId NOT IN (10,18) ORDER BY hit DESC  LIMIT " . $startRecord . ", " . $numberRecordPerPage;
                $command = Yii::app()->db_kkt->createCommand($sql);
                $rows = $command->queryAll();
                Yii::app()->cache->set($key, $rows, ConstantsUtil::TIME_CACHE_1800); 
            }else{ 
                $rows=$cache;  
            }
            return $rows;
        }
        public function getTotalVideo()
        {
            $sql = "SELECT count(id) FROM c_video WHERE  status=1 AND categoryId NOT IN (10,18)";
            $command = Yii::app()->db_kkt->createCommand($sql);
            $rows = $command->queryRow();        
            return $rows["count(id)"];
        }
        /* Thông tin về video */
        public function getInfoVideo($id)
        {
            $sql = "SELECT * FROM c_video WHERE id=" . $id;
            $command = Yii::app()->db_kkt->createCommand($sql);
            $row = $command->queryRow();  
            return $row;   
        }
        public function getLatestVideo(){
            $sql="SELECT id, title,picture,introtext,file,file_mp4,alias,picture,hit,download,duration,isHot,create_date FROM c_video WHERE status = 1 ORDER BY create_date DESC LIMIT 1";
            $command = Yii::app()->db_kkt->createCommand($sql);
            $row = $command->queryRow();  
            return $row;
        }
        public function getVideoCategory(){
            $sql="SELECT * FROM c_category WHERE status = 1 AND type=2 AND id NOT IN (10,18)";
            $command = Yii::app()->db_kkt->createCommand($sql);
            $rows = $command->queryAll();  
            return $rows;
        }
        public function getVideoCategoryDetail($catId){
            $sql="SELECT * FROM c_category WHERE id=".$catId;
            $command = Yii::app()->db_kkt->createCommand($sql);
            $row = $command->queryRow();  
            return $row;
        }
        public function getVideoByCat($catId,$currentPage,$numberRecordPerPage){
            $cacheService = new CacheService("Clip","getVideoByCat",$currentPage.$numberRecordPerPage);
            $key = $cacheService->createKey();
            $cache = Yii::app()->cache->get($key);
            if ($cache == false) 
            {
                $startRecord = ($currentPage - 1) * $numberRecordPerPage;  
                $sql="SELECT * FROM c_video WHERE status = 1 AND categoryId=".$catId." LIMIT " . $startRecord . ", " . $numberRecordPerPage;;
                $command = Yii::app()->db_kkt->createCommand($sql);
                $rows = $command->queryAll();  
            }else{ 
                $rows=$cache;  
            }
            return $rows;
        }
        public function getTotalVideoByCat($catId)
        {
            $sql = "SELECT count(id) FROM c_video WHERE  status=1 AND categoryId= ".$catId;
            $command = Yii::app()->db_kkt->createCommand($sql);
            $rows = $command->queryRow();        
            return $rows["count(id)"];
        }
        public function getCategoryIdByVideoId($id)
        {
            $sql = "SELECT categoryId FROM c_video WHERE  id= ".$id;
            $command = Yii::app()->db_kkt->createCommand($sql);
            $row = $command->queryRow();        
            return $row["categoryId"];
        }
        public function getVideoRandomByCatId($catId)
        {
            $sql = "SELECT id, title,picture,introtext,file,file_mp4,alias,picture,hit,download,duration,isHot,create_date FROM c_video WHERE  categoryId= ".$catId." ORDER BY RAND() LIMIT 0,3";
            $command = Yii::app()->db_kkt->createCommand($sql);
            $rows = $command->queryAll();     
            return $rows;
        }
    } 
?>
